﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_TempLateDAL : IDcms_TempLateDAL
    {
        public Dcms_TempLateModel AddModel(Dcms_TempLateModel model)
        {
            string sQLString = "insert into Dcms_TempLate(Template_Directory,Template_Name,Template_Author,Template_CreateTime,Template_UpdateTime,Template_State,Template_Ver,Template_Fordntver,Template_Copyright) values(@Template_Directory,@Template_Name,@Template_Author,@Template_CreateTime,@Template_UpdateTime,@Template_State,@Template_Ver,@Template_Fordntver,@Template_Copyright) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Template_Directory", SqlDbType.VarChar), new SqlParameter("@Template_Name", SqlDbType.VarChar), new SqlParameter("@Template_Author", SqlDbType.VarChar), new SqlParameter("@Template_CreateTime", SqlDbType.VarChar), new SqlParameter("@Template_UpdateTime", SqlDbType.VarChar), new SqlParameter("@Template_State", SqlDbType.Int), new SqlParameter("@Template_Ver", SqlDbType.VarChar), new SqlParameter("@Template_Fordntver", SqlDbType.VarChar), new SqlParameter("@Template_Copyright", SqlDbType.VarChar) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Template_Directory;
            cmdParms[2].Value = model.Template_Name;
            cmdParms[3].Value = model.Template_Author;
            cmdParms[4].Value = model.Template_CreateTime;
            cmdParms[5].Value = model.Template_UpdateTime;
            cmdParms[6].Value = model.Template_State;
            cmdParms[7].Value = model.Template_Ver;
            cmdParms[8].Value = model.Template_Fordntver;
            cmdParms[9].Value = model.Template_Copyright;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Template_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_TempLateModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_TempLateModel> list = new List<Dcms_TempLateModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_TempLateModel item = new Dcms_TempLateModel();
                if (ds.Tables[0].Rows[i]["Template_Id"].ToString() != "") item.Template_Id = int.Parse(ds.Tables[0].Rows[i]["Template_Id"].ToString());
                item.Template_Directory = ds.Tables[0].Rows[i]["Template_Directory"].ToString();
                item.Template_Name = ds.Tables[0].Rows[i]["Template_Name"].ToString();
                item.Template_Author = ds.Tables[0].Rows[i]["Template_Author"].ToString();
                item.Template_CreateTime = ds.Tables[0].Rows[i]["Template_CreateTime"].ToString();
                item.Template_UpdateTime = ds.Tables[0].Rows[i]["Template_UpdateTime"].ToString();
                if (ds.Tables[0].Rows[i]["Template_State"].ToString() != "") item.Template_State = int.Parse(ds.Tables[0].Rows[i]["Template_State"].ToString());
                item.Template_Ver = ds.Tables[0].Rows[i]["Template_Ver"].ToString();
                item.Template_Fordntver = ds.Tables[0].Rows[i]["Template_Fordntver"].ToString();
                item.Template_Copyright = ds.Tables[0].Rows[i]["Template_Copyright"].ToString();
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_TempLate where Template_Id=@templateid";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@templateid", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public int GetMaxId()
        {
            int num = 0;
            string maxNoteID = SqlHelper.GetMaxNoteID("templateid", "Dcms_TempLate");
            if (maxNoteID.Length > 0) num = Convert.ToInt32(maxNoteID);
            return num;
        }

        public Dcms_TempLateModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_TempLate where Template_Id=@templateid";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@templateid", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_TempLateModel model = new Dcms_TempLateModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_TempLateModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataTable GetModelList()
        {
            string sql = "select * from Dcms_TempLate";
            return SqlHelper.CreateDataSet(sql).Tables[0];
        }

        public Dcms_TempLateModel Update(Dcms_TempLateModel model)
        {
            string sQLString = "update Dcms_TempLate set Template_Directory=@Template_Directory,Template_Name=@Template_Name,Template_Author=@Template_Author,Template_CreateTime=@Template_CreateTime,Template_UpdateTime=@Template_UpdateTime,Template_State=@Template_State,Template_Ver=@Template_Ver,Template_Fordntver=@Template_Fordntver,Template_Copyright=@Template_Copyright where Template_Id=@Template_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Template_Directory", SqlDbType.VarChar), new SqlParameter("@Template_Name", SqlDbType.VarChar), new SqlParameter("@Template_Author", SqlDbType.VarChar), new SqlParameter("@Template_CreateTime", SqlDbType.VarChar), new SqlParameter("@Template_UpdateTime", SqlDbType.VarChar), new SqlParameter("@Template_State", SqlDbType.Int), new SqlParameter("@Template_Ver", SqlDbType.VarChar), new SqlParameter("@Template_Fordntver", SqlDbType.VarChar), new SqlParameter("@Template_Copyright", SqlDbType.VarChar), new SqlParameter("@Template_Id", SqlDbType.Int) };
            cmdParms[0].Value = model.Template_Directory;
            cmdParms[1].Value = model.Template_Name;
            cmdParms[2].Value = model.Template_Author;
            cmdParms[3].Value = model.Template_CreateTime;
            cmdParms[4].Value = model.Template_UpdateTime;
            cmdParms[5].Value = model.Template_State;
            cmdParms[6].Value = model.Template_Ver;
            cmdParms[7].Value = model.Template_Fordntver;
            cmdParms[8].Value = model.Template_Copyright;
            cmdParms[9].Value = model.Template_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

